package mysql

import (
	"time"

	"github.com/go-xorm/xorm"
	"github.com/spf13/viper"
)

func engine(key string) (*xorm.EngineGroup, error) {
	DSN := make([]string, 0)
	DSN = append(DSN, viper.GetString("mysql.db_"+key+"_m"))
	for _, v := range viper.GetStringSlice("mysql.db_" + key + "_s") {
		DSN = append(DSN, v)
	}
	cfg := &config{
		Key:             key,
		DSN:             DSN,
		MaxOpen:         10,
		MaxIdle:         10,
		ConnMaxLifetime: 30 * time.Second,
	}

	return mysqlEngine(cfg)
}

//不带分库的
func PublicEngine() (*xorm.EngineGroup, error) {
	key := "public_esf_leju_com"
	return engine(key)
}

//带分库的
func FangyouEngine(citycode string) (*xorm.EngineGroup, error) {
	key := ""
	switch citycode {
	case "bj":
		key = "db_fangyou_" + citycode

	case "sh":
		key = "db_fangyou_" + citycode

	}
	return engine(key)
}

func KongEngine() (*xorm.EngineGroup, error) {
	key := "kong"
	return engine(key)
}

func KongcrmEngine() (*xorm.EngineGroup, error) {
	key := "kongcrm"
	return engine(key)
}

func BargainEngine() (*xorm.EngineGroup, error) {
	key := "bargain_esf_leju_com"
	return engine(key)
}

func PoolEngine() (*xorm.EngineGroup, error) {
	key := "pool_esf_leju_com"
	return engine(key)
}

func Fetch3partDataEngine() (*xorm.EngineGroup, error) {
	key := "fetch_3part_data"
	return engine(key)
}

func EtcHouseEngine() (*xorm.EngineGroup, error) {
	key := "etc_house"
	return engine(key)
}
